﻿using Kristall;
using System.Threading;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using Microsoft.Xna.Framework.Content;
using Kristall.Managers;


namespace Kristall.Screens
{
    class LoadingScreen : GameScreen
    {
        GameScreen _screenToLoad;
        bool _loadingCompleted = false;
        bool _showStats = false;
        Texture2D background;

        public event EventHandler OnLoadingCompleted;

        public LoadingScreen(GameScreen screenToLoad, bool showStats = false)
        {
            TransitionOnTime = TransitionOffTime = TimeSpan.FromSeconds(0.2f);

            _screenToLoad = screenToLoad;
            _showStats = showStats;
        }

        Thread _loadingThread;
        bool _otherScreensAreGone = false;

        public override void LoadContent()
        {
            base.LoadContent();

            ContentManager contentManager = new ContentManager(ScreenManager.Game.Services, "Content");

            background = contentManager.Load<Texture2D>("Backgrounds/Loading");
            

            _screenToLoad.ScreenManager = ScreenManager;

            foreach (GameScreen screen in ScreenManager.GetScreens())
                screen.ExitScreen();

            _loadingThread = new Thread(delegate()
            {
                _screenToLoad.LoadContent();
                //Thread.Sleep(5000);
                _loadingCompleted = true;
            });
        }

        public override void HandleInput(InputState input, GameTime gameTime)
        {
            base.HandleInput(input, gameTime);

            if (!IsActive)
                return;

            if (_showStats && _loadingCompleted && input.IsMenuSelect)
                ExitScreen();
        }


        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            //Wait for other screens to transition off
            if (_otherScreensAreGone == false)
            {
                _otherScreensAreGone = ScreenManager.GetScreens().Length == 1;

                //If all screens are gone, start loading
                if (_otherScreensAreGone)
                    _loadingThread.Start();
                else
                    return;
            }

            //Only update transition once other screens are gone
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
   

            if (_loadingCompleted && _showStats == false && !IsExiting)
            {
                this.ExitScreen();

                if (OnLoadingCompleted != null)
                    OnLoadingCompleted(this, null);
            }

            //If we have completely transitioned off, add the the screen
            if (IsExiting && ScreenState == ScreenState.Hidden)
            {
                ScreenManager.AddScreen(_screenToLoad, false);
            }
        }

        float timer = 0;
        string dots = "";

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (!_otherScreensAreGone)
            {
                return;
            }

            SpriteBatch batch = ScreenManager.SpriteBatch;

            batch.Begin();

            batch.Draw(background, ScreenManager.Game.GraphicsDevice.Viewport.Bounds, Color.White);
            Vector2 centerScreen = Kristall.Viewport * 0.5f;

            if (_loadingCompleted && _showStats)
            {
                string msg = "Press Enter";
                Vector2 size = ScreenManager.Font.MeasureString(msg);
                batch.DrawString(ScreenManager.Font, msg, centerScreen - size * 0.5f, Color.White);
            }
            else
            {
                timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (timer >= 0.1f)
                {
                    dots += ".";
                    timer = 0;

                    if (dots.Length > 3)
                        dots = "";
                }

                string msg = "Loading" + dots;
                Vector2 size = ScreenManager.Font.MeasureString("Loading...");
                batch.DrawString(ScreenManager.Font, msg, centerScreen - size * 0.5f, Color.White);
            }

            batch.End();

            base.Draw(gameTime);
        }
    }
}
